package com.springcloud.sc.ucpv5.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.springcloud.sc.ucp.v5.api.mongo.locationdb.RoleInfo;
import com.springcloud.sc.ucp.v5.api.vo.RoleInfoQueryReq;
import com.springcloud.sc.ucp.v5.api.vo.RoleOperationReq;
import com.springcloud.sc.ucpv5.service.UcpRoleService;
import com.springcloud.sc.ucpv5.util.OperationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("role")
public class UcpRoleController {

    @Autowired
    private UcpRoleService ucpRoleService;

    @ResponseBody
    @RequestMapping(value = "/RoleQuery", method = RequestMethod.POST )
    public String roleQuery(@RequestParam("content") String content){
       RoleInfoQueryReq roleInfoQueryReq = JSON.parseObject(content, RoleInfoQueryReq.class);
       List list = ucpRoleService.getRoleDataInfo(roleInfoQueryReq);
        OperationResult result_info = new OperationResult();
        if (list == null) {
            result_info.errorCode = 1;
            result_info.errorDesc = "查询出现异常";
        } else {
            result_info.resultContent = JSONObject.toJSONString( list );
        }
        String jsonObject = JSONObject.toJSONString( result_info );
       return jsonObject;
    }

    @ResponseBody
    @PostMapping(value = "/RoleOper")
    public String roleOper(@RequestParam("content") String content){
        //调度台原接口只识别String类型的返回数据，所以需要手动转换
        RoleOperationReq roleOperationReq = JSON.parseObject(content, RoleOperationReq.class);
        RoleInfo role_return = null;
        if (roleOperationReq.opercmd == 0) {
            role_return = ucpRoleService.saveRole( roleOperationReq.role_info );
        } else if (roleOperationReq.opercmd == 1) {
            role_return = ucpRoleService.updateRole(roleOperationReq.role_info);
        } else if (roleOperationReq.opercmd == 2) {
            role_return = ucpRoleService.deleteRole(roleOperationReq.role_info);
        }
        OperationResult result_info = new OperationResult();
        if (role_return == null) {
            result_info.errorCode = 1;
            result_info.errorDesc = "操作出现异常";
        } else {
            result_info.resultContent = JSONObject.toJSONString(role_return);
        }
        String jsonObject = JSONObject.toJSONString( result_info );

        return jsonObject;
    }
}
